function getlinepricetype(v)
{
	var tmp=v+',';
	if (!tmp.match(new RegExp('^(([0-9]+),)+$', 'i')) == false) {
		return 1;
	} else if (!tmp.match(new RegExp('^([一二三四五六日],)+$', 'i')) == false) {
		return 2;
	} else if(v.indexOf("至")!=-1){
		return 3;
	}
	return 0;
}
function formatlinestartdate(){
	//格式化发团日期
	$('[linestartdate]').each(function(){
		var v=$(this).attr('linestartdate');
		var t=getlinepricetype(v);
		if (t==1) {
			v='每月'+v.replace(/,/g, '号,')+'号';
		} else if (t==2) {
			v='每周'+v;
		} else if(t==3){
			var l=v.indexOf("至");
			var d1=new Date(Date.parse($.trim(v.substring(0, l)).replace(/-/g, '/')));
			var d2=new Date(Date.parse($.trim(v.substring(l+1)).replace(/-/g, '/')));
			v=d1.getFullYear()+'年'+(d1.getMonth()+1)+'月'+d1.getDate()+'日~'+d2.getFullYear()+'年'+(d2.getMonth()+1)+'月'+d2.getDate()+'日';
		}
		t=v.split(',');
		if(t.length>4){
			t.length=4;
			v=t.join(',')+'...';
		}
		$(this).append(v.replace(/,/g, '、'));
	});
}
$(function () {
	formatlinestartdate();
	//满意度
	$('[linereviewss]').each(function(){
		var r=parseInt($(this).attr('linereviewss'));
		var h=parseInt($(this).attr('haoping'));
		$(this).html((r==0?100:Math.round(h/r*100))+'%');
	});
});
var LineCalendar=new Array();
function CalendarPage(c,y,m){
	CreateCalendar(c,y,m,LineCalendar[c][3],LineCalendar[c][4],LineCalendar[c][5],LineCalendar[c][6],LineCalendar[c][7]);	
}
function CreateCalendar(c,y,m,a,b,d,e,fu) {//c:容器,y:年,m:月,a:出发时间,b:成人价,d:儿童价,e:特殊价格,fu:回调
	if(!e)e='';
	LineCalendar[c]=[c,y,m,a,b,d,e,fu];
    var today = new Date(); //今天
	today = new Date(Date.parse(today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate()));
	if(y==0||m==0){y=today.getFullYear();m=today.getMonth() + 1;}
   	var dmax=today.DateAdd('d',90).DateToParse(); //编辑价格的日期范围
    var dmin=today.DateAdd('d',1).DateToParse();
	var i1=0,i2=0,i3=0,d1=new Date(dmin),d2=new Date(),t=getlinepricetype(a);
	var obj,arr1,arr2,str1='';
	var tmp=a+',';
	if (t==1) {
		i1=(new Date(Date.parse(d1.getFullYear() + '/' + (d1.getMonth() + 2) + '/1') - 86400000)).getDate();
		i2=d1.getDate();
		arr1=a.split(",");
		for(var i=0;i<arr1.length;i++){
			if(i1>=parseInt(arr1[i])&&i2<=parseInt(arr1[i])){i3=parseInt(arr1[i]);break;}
		}
		if(i3>0){
			dmin =Date.parse(d1.getFullYear() + '/' + (d1.getMonth() + 1) + '/' + i3);
		}else{
			i1=parseInt(arr1[0]);
			for(var i=2;i<5;i++){
				d2 =new Date(Date.parse(d1.getFullYear() + '/' + (d1.getMonth() + i) + '/' + i1));
				if(d2.getMonth()+1==d1.getMonth()+i){
					dmin =Date.parse(d2.getFullYear() + '/' + (d2.getMonth() + 1) + '/' + d2.getDate());
					break;
				}
			}
		}
	} else if (t==2) {
		var week=['日','一','二','三','四','五','六'];
		if(tmp.indexOf(week[d1.getDay()])==-1){
			for(var i=1;i<7;i++){
				d2=d1.DateAdd('d',i);
				if(tmp.indexOf(week[d2.getDay()])!=-1){dmin=d2.DateToParse();break;}	
			}
		}
	} else if(t==3){
		i1=a.indexOf("至");
		i2=Date.parse($.trim(tmp.substring(0, i1)).replace(/-/g, '/'));
		dmax=Date.parse($.trim(tmp.substring(i1+1)).replace(/-/g, '/'));
		if(i2>dmin)dmin=i2;
	}
    today = today.DateToParse();
	d1=new Date(dmin);
	if(Date.parse(d1.getFullYear() + '/' + (d1.getMonth() + i) + '/1') > Date.parse(y + '/' + m + '/1')){
		y=d1.getFullYear();m=d1.getMonth() + 1;
	}
    $('#'+c).html('');
    //当前月的天数
    tmp = '<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#CCCCCC" class="lineDate">';
    tmp += '  <tr class="lineDateTit">';
    tmp += '    <td width="12%" style="color:#f60">星期日</td>';
    tmp += '    <td width="12%">星期一</td>';
    tmp += '    <td width="12%">星期二</td>';
    tmp += '    <td width="12%">星期三</td>';
    tmp += '    <td width="12%">星期四</td>';
    tmp += '    <td width="12%">星期五</td>';
    tmp += '    <td width="12%" style="color:#f60">星期六</td>';
    tmp += '    <td width="16%">月份</td>';
    tmp += '  </tr>';
    var maxdays = (new Date(Date.parse(y + '/' + (m + 1) + '/1') - 86400000)).getDate();
    d1 = new Date(Date.parse(y + '/' + m + '/1'));//要显示的日期
    i1 = d1.getDay();//这个月的第一天是星期几
    for (var j = 1; j <= 6; j++) {
        tmp += '<tr>';


        for (var k = 1; k <= 7; k++) {
            i2 = (j - 1) * 7 + k - i1;
            if (i2 < 1 || i2 > maxdays) {
                tmp += '<td></td>';
            } else {
                i3 = Date.parse(y + '/' + m + '/' + i2);
                if (i3 < dmin || i3 > dmax) {
                    if (today == i3) {
                    	tmp += '<td><font color="red">' + i2 + '</font><br />今天</td>';
                    } else {
                    	tmp += '<td style="color:#999999">' + i2 + '</td>';
                    }
				}else {
                    tmp += '<td week="' + (k - 1) + '" id="' + y + '-' + m + '-' + i2 + '">' + i2 + '</td>';
                }
            }
        }
		if(j==1){
			tmp += '<td rowspan="6" style="background:#f5f5f5;">';
			d1 = new Date(today);
			if(d1.getFullYear()==y&&(d1.getMonth() + 1)==m){
				tmp += '<em class="upnone"></em>';
			}else{
				d1=new Date(Date.parse(y + '/' + (m-1) + '/1'));
				tmp += '<em class="up" onclick="CalendarPage(\''+c+'\','+d1.getFullYear()+','+(d1.getMonth()+1)+');"></em>';
			}
			tmp += '<p>'+y+'年<br />'+m+'月</p>';
			d1=new Date(Date.parse(y + '/' + (m+1) + '/1'));
			tmp += '<em class="dowm" onclick="CalendarPage(\''+c+'\','+d1.getFullYear()+','+(d1.getMonth()+1)+');"></em></td>';
		}
        tmp += '</tr>';
    }
    tmp += '</table>';
    $('#'+c).append(tmp);
	switch (t) {
        case 1:
            {
				arr1=a.split(',');
				for(var i=0;i<a.length;i++){
					str1 += '[id$="-' + arr1[i] + '"],';
				}
				str1 = str1.substring(0, str1.length - 1);
				obj = $('#'+c).find(str1); break;
            }
        case 2:{
				tmp="日一二三四五六";
				arr1=a.split(',');
				for(var i=0;i<a.length;i++){
					i1=tmp.indexOf(arr1[i]);
					if(i1!=-1)str1+='[week="' + i1 + '"],';
				}
				str1 = str1.substring(0, str1.length - 1);
				obj = $('#'+c).find(str1); break;
            }
        default:{obj=$('#'+c).find('[week]'); break;}
    }
	$(obj).attr({'price':b,'childrenprice':d});
	var obj1;
	if(e!=''){
		arr1=e.split('|');
		for(var i=0;i<arr1.length;i++){
			arr2=arr1[i].split(':');
			obj1=$('#'+c).find('#'+arr2[0]);
			if(obj1.length>0)$(obj1).attr({'price':arr2[1],'childrenprice':arr2[2]});
		}
	}
	$(obj).each(function(){
		if($(this).attr('price')==0){
			$(this).append('<br />已满')	;
		}else{
			$(this).append('<br /><b class="money">'+$(this).attr('price')+'</b>元');
		}				 
	});
	if($.isFunction(fu))$(obj).filter('[price!="0"]').css('cursor','pointer').click(function(){fu(this);});
}